package com.itbaizhan.openhis.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbaizhan.openhis.constant.Constants;
import com.itbaizhan.openhis.domain.User;
import com.itbaizhan.openhis.dto.UserDto;
import com.itbaizhan.openhis.mapper.UserMapper;
import com.itbaizhan.openhis.service.UserService;
import com.itbaizhan.openhis.utils.AppMd5Utils;
import com.itbaizhan.openhis.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 *
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;
    //根据手机号查询用户
    @Override
    public User queryUserByPhone(String phone) {

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq(User.COL_PHONE,phone);
        User user = userMapper.selectOne(wrapper);
        return user;


    }

    @Override
    public DataGridView listUserForPage(UserDto userDto) {
        Page<User> page = new Page<>(userDto.getPageNum(),userDto.getPageSize());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(userDto.getUserName()),User.COL_USER_NAME,userDto.getUserName());
        wrapper.like(StringUtils.isNotBlank(userDto.getPhone()),User.COL_PHONE,userDto.getPhone());
        wrapper.eq(StringUtils.isNotBlank(userDto.getStatus()),User.COL_STATUS,userDto.getStatus());
        wrapper.eq(userDto.getDeptId() != null,User.COL_DEPT_ID,userDto.getDeptId());
        wrapper.ge(userDto.getBeginTime() !=null,User.COL_CREATE_TIME,userDto.getBeginTime());
        wrapper.le(userDto.getEndTime() != null,User.COL_CREATE_TIME,userDto.getEndTime());
        wrapper.orderByAsc(User.COL_USER_ID);
        userMapper.selectPage(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());



    }

    @Override
    public int addUser(UserDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto,user);
        user.setUserType(Constants.USER_NORMAL);
        String defaultPwd = user.getPhone().substring(5); //获取电话号码的后6位
        user.setCreateBy(userDto.getSimpleUser().getUserName());
        user.setCreateTime(DateUtil.date());
        user.setSalt(AppMd5Utils.md5(defaultPwd,user.getSalt(),2));
        return userMapper.insert(user);
    }

    @Override
    public int updateUser(UserDto userDto) {
        //查出要更新的用户
        User user = userMapper.selectById(userDto.getUserId());
        if(user == null ){
            return 0;
        }
        BeanUtils.copyProperties(userDto,user);
        user.setUpdateBy(userDto.getSimpleUser().getUserName());

        return userMapper.updateById(user);
    }

    @Override
    public int deleteUser(Long userIds) {
        List<Long> ids = Arrays.asList(userIds);
        if(ids != null && ids.size()>0){
            return userMapper.deleteBatchIds(ids);
        }else {
            return 0;
        }

    }

    @Override
    public void resetPassword(Long[] userIds) {
        for(Long userId: userIds){
            User user = userMapper.selectById(userId);
            String defaultPwd = "";
            //如果查询到的用户是管理员，默认密码设为123456，如果是普通的设为手机号的后6位
            if(user.getUserType().equals(Constants.USER_ADMIN)){
                defaultPwd="123456";
            }else {
                defaultPwd=user.getPhone().substring(5);
            }
            user.setSalt(AppMd5Utils.createSalt());
            user.setPassword(AppMd5Utils.md5(defaultPwd,user.getSalt(),2));
            this.userMapper.updateById(user);
        }
    }

    @Override
    public List<User> queryUsersNeedScheduling(Long userId,Long deptId) {
       QueryWrapper<User> wrapper = new QueryWrapper<>();
       wrapper.eq(deptId != null,User.COL_DEPT_ID,deptId);
        wrapper.eq(userId != null,User.COL_USER_ID,userId);
        wrapper.eq(User.COL_SCHEDULING_FLAG,Constants.SCHEDULING_FLAG_TRUE);  //0 -需要排班
       wrapper.eq(User.COL_STATUS,Constants.STATUS_TRUE); //0 - 正常用户
        return userMapper.selectList(wrapper);
    }
}




